﻿using System.ComponentModel;
using System.Reflection;
using System.Windows;
using System;
#if NETFX_CORE
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml;
using Windows.ApplicationModel.DataTransfer;
#else
using System.Windows.Controls;
#endif

namespace UnitDriven
{
	public sealed partial class TestEngine :
#if WINDOWS_PHONE
        Microsoft.Phone.Controls.PhoneApplicationPage
#elif NETFX_CORE
        Page
#else
        UserControl
#endif
	{
		private TestContext _context;
		public TestContext Context
		{
			get { return _context; }
		}

        public TestEngine()
        {
            InitializeComponent();
        }

#if !NETFX_CORE
		public TestEngine(Assembly assembly)
			: this(new TestContext(assembly))
		{
		}

		public TestEngine(TestContext context)
		{
			if (!DesignerProperties.GetIsInDesignMode(this))

			{
				this._context = context;
				DataContext = Context;

				InitializeComponent();
			}
		}
#endif
        private void RunAll_Click(object sender, RoutedEventArgs e)
		{
#if NETFX_CORE
            this.Focus(FocusState.Programmatic);
#else
			this.Focus();
#endif
			_context.Run();
		}

        private void Run_Click(object sender, RoutedEventArgs e)
		{
			Button b = (Button)sender;
			var tester = (IRunTests)b.DataContext;
			tester.RunTests();
		}

        private void SummaryCopy_Click(object sender, RoutedEventArgs e)
		{
			var summary = this._context.ToSummary();
            if (System.Diagnostics.Debugger.IsAttached)
            {
                System.Diagnostics.Debugger.Break();
                System.Diagnostics.Debug.WriteLine(summary);
            }

#if NETFX_CORE
            var data = new DataPackage();
            data.SetText(summary);
            Clipboard.SetContent(data);
#else
			System.Windows.Clipboard.SetText(summary);
#endif
		}

#if WINDOWS_PHONE
		private void ErrorDetails_Click(object sender, RoutedEventArgs e)
		{
			Button b = (Button)sender;
			var tester = (IRunTests)b.DataContext;
			var details = new ErrorDetails(tester, this._context);
			Application.Current.RootVisual = details;
		}

		private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
		{
			if (filterText.Text.EndsWith("\r"))
			{
				this.Context.Filter = this.filterText.Text;
				this.filterText.Text = this.filterText.Text.TrimEnd('\r');
				this.filterText.SelectAll();
				this.Focus();
			}
		}
#elif NETFX_CORE
        private void ErrorDetails_Click(object sender, RoutedEventArgs e)
        {
            Button b = (Button)sender;
            var tester = (IRunTests)b.DataContext;
            var details = new ErrorDetails(tester, this._context);
            Window.Current.Content = details;
        }

        //private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        //{
        //    if (filterText.Text.EndsWith("\r"))
        //    {
        //        this.Context.Filter = this.filterText.Text;
        //        this.filterText.Text = this.filterText.Text.TrimEnd('\r');
        //        this.filterText.SelectAll();
        //        this.Focus(FocusState.Programmatic);
        //    }
        //}

        protected override void OnNavigatedTo(Windows.UI.Xaml.Navigation.NavigationEventArgs e)
        {
            this.DataContext = this._context = (TestContext)e.Parameter;
            base.OnNavigatedTo(e);
        }
#else
		private void ErrorDetails_Click(object sender, RoutedEventArgs e)
		{
			Button b = (Button)sender;
			dynamic test = b.DataContext;
			System.Windows.Clipboard.SetText(test.Message);
		}

		private void TextBox_TextInput(object sender, System.Windows.Input.TextCompositionEventArgs e)
		{
			this.Context.Filter = this.filterText.Text;
			this.filterText.SelectAll();
		}
#endif
        private void TextBox_GotFocus(object sender, RoutedEventArgs e)
        {
            //this.filterText.SelectAll();
        }
	}
}
